#include "Square.h"


Square::Square(){}

Square::Square(std::string id, Point vertex, int side, Color *borderColor):Figure(id){
	this->side=side;
	this->borderColor = borderColor;
	this->fillColor=NULL;
	this->texture=NULL;
	this->setLimits(vertex);
}

Square::Square(std::string id, Point vertex,int side, Color *lineColor, Color* fillColor){
	this->side=side;
	if(lineColor==NULL)
		this->borderColor = new Color(*fillColor);
	else
		this->borderColor =lineColor;
	this->fillColor=fillColor;
	this->id=id;
	this->texture=NULL;
	this->setLimits(vertex);
}

Square::Square(std::string id, Point vertex,int side, Color *borderColor, Texture* texture){
	this->side=side;
	this->borderColor = borderColor;
	this->fillColor=NULL;
	this->id=id;
	this->texture=texture;
	this->setLimits(vertex);

}

Square::Square(std::string id, Point vertex,int side, Texture* texture){
	this->side=side;
	this->borderColor = NULL;
	this->fillColor=NULL;
	this->id=id;
	this->texture=texture;
	this->setLimits(vertex);
}

void Square::setLimits(Point vertex){
	//seteo los cuatro puntros del cuadrado
	Point aux(0,0);
	aux.setY(this->side);
	this->pointA = vertex - aux;
	aux.setY(0);
	aux.setX(this->side);
	this->pointB = pointA + aux;   
	this->pointC = vertex;
	this->pointD = vertex + aux;
}


void Square::draw() throw (DrawingExcep){
	if(fillColor!=NULL)
		SurfaceContainer::getInstance()->fillRectangle(this->pointC,this->side,this->side,this->fillColor->getSDLColor());
	
	if(texture!=NULL)
		drawTextured();

	if(borderColor!=NULL)
		SurfaceContainer::getInstance()->drawRectangle(pointA,pointB,pointD,pointC,this->borderColor->getSDLColor());
	
}


void Square::drawTextured(){
	
	int posicion =0;
	//seteo los puntos de la imagen, textura
	Point aux(0,0);
	Point a_bmp = aux;
	aux.setX(texture->getImage()->getImageWidth());
	Point b_bmp = aux;
	aux.setX(0);
	aux.setY(texture->getImage()->getImageHeight());
	Point c_bmp = aux;
	aux.setX(texture->getImage()->getImageWidth());
	Point d_bmp = aux;

	if (SurfaceContainer::getInstance()->pointInScreen(pointC)){
	//if (SurfaceContainer::getInstance()->acceptLimits(pointC,this->side,this->side)){
		SurfaceContainer::getInstance()->fillTexture(this->texture->getImage(),pointA,pointD,pointC,a_bmp,d_bmp,c_bmp,posicion);
		SurfaceContainer::getInstance()->fillTexture(this->texture->getImage(),pointA,pointB,pointD,a_bmp,b_bmp,d_bmp,posicion);
	}
	else
		throw DrawingExcep(MsjExcep::NotAcceptLimits(pointC));
}

std::string Square::getCreationLog(){
	std::string msj = "Se creo un Cuadrado id: ";
	std::stringstream ss; 
	ss << id;
	msj+= ss.str();
	msj.append(". Lado: ");
	ss.str("");
	ss<< side;
	msj += ss.str();
	msj.append(" , Vertice: [");
	ss.str("");
	ss<< pointC.getX();
	msj += ss.str();
	ss.str("");
	msj.append(",");
	ss<< pointC.getY();
	msj += ss.str();
	msj.append("]");

	return msj;
}

Square::~Square(){}